#include "InPerLed.h"

#define DEF_TAG         "Led"

Com_u8 g_InPerLed_Exit_Flag = DEF_COM_FALSE;
Com_u8 g_InPerLed_Sta = DEF_COM_FALSE;

void Ext_InPerLed_SetExitFlag(Com_u8 flag)
{
    g_InPerLed_Exit_Flag = flag;
}

void Ext_InPerLed_SetSta(Com_u8 flag)
{
    g_InPerLed_Sta = flag;
}

void Ext_InPerLed_Set(int stat)
{
    switch(stat)
    {
    case LED_STATUS_ON:
    {
#if CMAKE_LED_ON_LOW_LEVEL
        Ext_SdkGpioWrite_Led(DEF_COM_LEVEL_LOW);
#else
        Ext_SdkGpioWrite_Led(DEF_COM_LEVEL_HIGHT);
#endif
    }
        break;

    case LED_STATUS_OFF:
    {
#if CMAKE_LED_ON_LOW_LEVEL
        Ext_SdkGpioWrite_Led(DEF_COM_LEVEL_HIGHT);
#else
        Ext_SdkGpioWrite_Led(DEF_COM_LEVEL_LOW);
#endif
    }
        break;

    case LED_STATUS_FAST:
        Ext_InPerLed_Set(LED_STATUS_ON);
        usleep(500 * 1000);
        Ext_InPerLed_Set(LED_STATUS_OFF);
        usleep(500 * 1000);
        Ext_InPerLed_Set(LED_STATUS_ON);
        break;

    case LED_STATUS_SLOW:
        Ext_InPerLed_Set(LED_STATUS_ON);
        usleep(1500 * 1000);
        Ext_InPerLed_Set(LED_STATUS_OFF);
        usleep(1500 * 1000);
        Ext_InPerLed_Set(LED_STATUS_ON);
        break;

    case LED_STATUS_1S:
        Ext_InPerLed_Set(LED_STATUS_ON);
        usleep(1 * 1000 * 1000);
        Ext_InPerLed_Set(LED_STATUS_OFF);
        usleep(1 * 1000 * 1000);
        Ext_InPerLed_Set(LED_STATUS_ON);
        break;

    case LED_STATUS_DOUBLE:
        Ext_InPerLed_Set(LED_STATUS_ON);
        usleep(100 * 1000);
        Ext_InPerLed_Set(LED_STATUS_OFF);
        usleep(100 * 1000);
        Ext_InPerLed_Set(LED_STATUS_ON);
        usleep(100 * 1000);
        Ext_InPerLed_Set(LED_STATUS_OFF);
        sleep(1);
        break;

    default:
        break;
    }
}

void Ext_InPerLed_CtrlStatus(Com_u8 flag)
{
    if(flag == DEF_COM_TRUE)
    {
        if (Ext_InComFile_Access(DEF_DP_LIGHT_LED) == DEF_COM_FILE_EXIST)
        {
            Ext_InPerLed_Set(LED_STATUS_ON);
        }
        else
        {
            Ext_InPerLed_Set(LED_STATUS_OFF);
        }
    }
    else
    {
#if CMAKE_LED_IS_WATCH_LED
        int i;
        for(i = 0;i < 2;i++)
        {
            Ext_InPerLed_Set(LED_STATUS_FAST);
        }
        usleep(500 * 1000);
#else
        Ext_InPerLed_Set(LED_STATUS_OFF);
#endif
    }
}

Com_pvoid InPerLed_Thread(Com_pvoid data)
{
    DEF_COM_SET_THREAD_NAME(DEF_TAG,g_ComThread_Num);

    g_InPerLed_Exit_Flag = DEF_COM_FALSE;
    g_InPerLed_Sta = DEF_COM_FALSE;

    while ((g_InPerLed_Exit_Flag != DEF_COM_TRUE) && g_ComThread_Running)
    {
        if ((g_InPerLed_Sta == DEF_COM_TRUE) ||
            (Ext_InComFile_Access(DEF_FLAG_NO_NETWORK) == DEF_COM_FILE_NOT_EXIST))
        {
            if (Ext_InComFile_Access(DEF_FLAG_WPA_PWD_ERROR) == 0)
            {
                Ext_InPerLed_Set(LED_STATUS_DOUBLE);
            }
            else
            {
                Ext_InPerLed_Set(LED_STATUS_FAST);
            }
        }
        else
        {
            /* ap mode */
            Ext_InPerLed_Set(LED_STATUS_SLOW);
        }
    }

    if (g_ComThread_Running == DEF_COM_TRUE)
    {
        /* rm no_network file */
        Ext_InComFile_JudgeRemove(DEF_FLAG_NO_NETWORK);
    }

    DEF_COM_EXIT_THREAD(g_ComThread_Num);
}

Com_void Ext_InPerLed_Init(Com_void)
{
    pthread_t id;

    pthread_create(&id, NULL,InPerLed_Thread, NULL);
    pthread_detach(id);
}
